00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 if ( !defined("PATH_TO_ROOT") ) {
00025 define( "PATH_TO_ROOT", "../../../" );
00026 }
00027
00028 require_once (PATH_TO_ROOT."ressourcen/classes/filemanager/class.resource.inc.php");
00029 require_once (PATH_TO_ROOT."ressourcen/classes/filemanager/class.file.inc.php");
00030 require_once (PATH_TO_ROOT."ressourcen/classes/filemanager/class.folder.inc.php");
00031
00032 require_once (PATH_TO_ROOT."ressourcen/classes/archiver/interface.resourcevisitor.inc.php");
00033
00040 class ZipVisitor implements ResourceVisitor {
00041
00042 private $recursion;
00043 private $archiver;
00044
00053 public function __construct($archiver, $recursion) {
00054 assert(!is_null($archiver));
00055 assert($archiver instanceof ZipArchive);
00056
00057 $this->archiver = $archiver;
00058 $this->recursion = $recursion;
00059 }
00060
00066 public function setRecursion($recursion) {
00067 $this->recursion = $recursion;
00068 }
00069
00075 public function getRecursion() {
00076 return $this->recursion;
00077 }
00078
00084 public function setArchiver($archiver) {
00085 $this->archiver = $archiver;
00086 }
00087
00093 public function getArchiver() {
00094 return $this->archiver;
00095 }
00096
00105 public function visit(Resource $resource, $parentPath = "") {
00106 assert(!is_null($resource));
00107
00108 if ($resource instanceof Folder) {
00109 $this->visitFolder($resource, $parentPath);
00110 } elseif ($resource instanceof File) {
00111 if ($resource->getType() == "file") {
00112 $this->visitFile($resource, $parentPath);
00113 } elseif ($resource->getType() == "link") {
00114 $this->visitLink($resource, $parentPath);
00115 } else {
00116 throw new ArchiverException("Unbekannter Typ des 'File'-Objekts");
00117 }
00118 }
00119 }
00120
00127 private function visitFolder(Folder $folder, $parentPath) {
00128 assert($folder instanceof Folder);
00129
00130
00131 $path = $parentPath.$folder->getLink();
00132
00133 $suffix = 0;
00134 while ($this->archiver->locateName($path, ZipArchive::FL_NOCASE) != FALSE) {
00135 if ($suffix == 0) {
00136 $pattern = "$";
00137 } else {
00138 $pattern = "-".$suffix."$";
00139 }
00140 $suffix++;
00141 $replacement = "-".$suffix;
00142
00143 $path = ereg_replace($pattern, $replacement, $filePath);
00144 }
00145 $path .= "/";
00146
00147 if (!$this->archiver->addEmptyDir($path)) {
00148 throw new ArchiverException("Verzeichnis '".$folder->getName().
00149 "' konnte nicht zum Archiv hinzugefügt werden");
00150 }
00151
00152 $this->addResourceDescription($folder, $path);
00153
00154 $parentPath = $path;
00155
00156 if ($this->recursion) {
00157 $subFolders = $folder->getSubFolders();
00158 foreach ($subFolders as $subFolder) {
00159 $subFolder->archiverAccept($this, $parentPath);
00160 }
00161 }
00162
00163 $files = $folder->getFiles();
00164 foreach ($files as $file) {
00165 $file->archiverAccept($this, $parentPath);
00166 }
00167 }
00168
00175 private function visitFile(File $file, $parentPath) {
00176 assert($file instanceof File);
00177
00178 $filePath = $parentPath.$file->getOrigFileName();
00179
00180 $suffix = 0;
00181 while ($this->archiver->locateName($filePath, ZipArchive::FL_NOCASE) != FALSE) {
00182 if ($suffix == 0) {
00183 $pattern = ".".$file->getExtension()."$";
00184 } else {
00185 $pattern = "-".$suffix.".".$file->getExtension()."$";
00186 }
00187 $suffix++;
00188 $replacement = "-".$suffix.".".$file->getExtension();
00189
00190 $filePath = ereg_replace($pattern, $replacement, $filePath);
00191 }
00192
00193 if (!$this->archiver->addFile($file->getPathToFile(), $filePath)) {
00194 throw new ArchiverException("Datei '".$file->getName().
00195 "' konnte nicht zum Archiv hinzugefügt werden");
00196 }
00197
00198 self::addResourceDescription($file, $filePath);
00199 }
00200
00207 private function visitLink(File $file, $parentPath) {
00208 assert($file instanceof File);
00209
00210 $filePath = $parentPath.$file->getName().".url";
00211
00212 $content = "[InternetShortcut]";
00213 $content .= "\r\n";
00214 $content .= "URL=".$file->getLink();
00215
00216 $suffix = 0;
00217 while ($this->archiver->locateName($filePath, ZipArchive::FL_NOCASE) != FALSE) {
00218 if ($suffix == 0) {
00219 $pattern = ".".$file->getExtension()."$";
00220 } else {
00221 $pattern = "-".$suffix.".".$file->getExtension()."$";
00222 }
00223 $suffix++;
00224 $replacement = "-".$suffix.".".$file->getExtension();
00225
00226 $filePath = ereg_replace($pattern, $replacement, $filePath);
00227 }
00228
00229 if (!$this->archiver->addFromString($filePath, $content)) {
00230 throw new ArchiverException("Datei '".$file->getName().
00231 "' konnte nicht zum Archiv hinzugefügt werden");
00232 }
00233
00234 $this->addResourceDescription($file, $filePath);
00235 }
00236
00243 private function addResourceDescription(Resource $resource, $path) {
00244
00245 $description = $resource->getDescription();
00246
00247 $description = iconv ("latin1", "ASCII//TRANSLIT", $description);
00248
00249 if (!$this->archiver->setCommentName($path, $description)) {
00250 throw new ArchiverException("Für die Ressource '".$resource->getName().
00251 "' konnte kein Kommentar hinzugefügt werden");
00252 }
00253 }
00254 }
00255
00256 ?>